package com.system.controller;



import com.system.po.*;
import com.system.service.AddresslistService;
import com.system.service.DocumentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/leader")
public class LeaderController {

    private static final Logger LOGGER=LoggerFactory.getLogger(LeaderController.class);

    @Resource
    private AddresslistService addresslistService;

    @Resource
    private DocumentService documentService;

    @GetMapping("/showAddressList")
    private String showAddressList(Model model, Integer page) throws Exception {
        List<AddresslistCustom> list = null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        //设置总数据量
        pagingVO.setTotalCount(addresslistService.getCountCouse());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = addresslistService.findByPaging(1);
        } else {
            pagingVO.setToPageNo(page);
            list = addresslistService.findByPaging(page);
        }
        /*System.out.println(list);*/

        model.addAttribute("addresslistList", list);
        model.addAttribute("pagingVO", pagingVO);

        return "leader/showAddressList";
    }

    //通讯录中搜索
    @RequestMapping(value = "/selectAddressList", method = {RequestMethod.POST})
    private String selectAddressList(String findByName, Model model) throws Exception {

        List<AddresslistCustom> list = addresslistService.findByName(findByName);

        model.addAttribute("addresslistList", list);
        return "leader/showAddressList";
    }

    //展示工作计划安排所有任务
    @RequestMapping(value = "/showDocument",method = RequestMethod.GET)
    private String showDocument(Model model, Integer page) throws Exception{

        List<Document> list = null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        //设置总数据量
        pagingVO.setTotalCount(documentService.getCountCouse());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = documentService.findAll(1);
        } else {
            pagingVO.setToPageNo(page);
            list = documentService.findAll(page);
        }
        /*System.out.println(list);*/

        model.addAttribute("documentList", list);
        model.addAttribute("pagingVO", pagingVO);

        return "leader/showDocument";
    }
    /**
     * 工作计划中按标题搜索
     * @param title
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping("/selectDocument")
    public String selectDocument(String title,Model model) throws Exception {
        List<Document> list=documentService.findByTitle(title);
        model.addAttribute("documentList",list);
        return "leader/showDocument";
    }

    /**
     * 添加工作计划页面显示
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addDocument1", method = {RequestMethod.GET})
    public String addStudentUI() throws Exception {

        return "leader/addDocument1";
    }

    /**
     * 添加工作计划
     * @param document
     * @return
     * @throws Exception
     */
    @PostMapping("/addDocument")
    public String addDocument(Document document) throws Exception{
        Date date=new Date(System.currentTimeMillis());
        document.setStarttime(date);
        document.setTag(0);
        documentService.save(document);
        return "redirect:/leader/showDocument";
    }


    // 修改工作计划页面显示
    @RequestMapping(value = "/editDocument", method = {RequestMethod.GET})
    public String editConditionUI(Integer id,Model model) throws Exception {
        if (id == null) {
            //没有带id就进来的话就返回显示页面
            return "redirect:/leader/showDocument";
        }
        Document document=documentService.findById(id);
        model.addAttribute("document",document);
        return "leader/editDocument";
    }

    // 修改工作计划
    @RequestMapping(value = "/editDocument", method = {RequestMethod.POST})
    public String editCondition(Document document) throws Exception {
        documentService.updateById(document);
        return "redirect:/leader/showDocument";
    }

    // 删除工作计划
    @RequestMapping(value = "/removeDocument", method = {RequestMethod.GET})
    public String removeDocument(Integer id) throws Exception {
        if (id == null){
            //没有带id就进来的话就返回显示页面
            return "redirect:/leader/showDocument";
        }
        documentService.deleteById(id);
        return "redirect:/leader/showDocument";
    }

    /**
     * 修改工作完成状态
     * @return
     * @throws Exception
     */
    @GetMapping("/editCondition")
    public String editCondition(Integer id) throws Exception{
        //必须进行一个判空，否则可能会导致更改失败，并且服务端不会报错
        if (id == null) {
            //没有带id就进来的话就返回显示页面
            return "redirect:/leader/showDocument";
        }
        documentService.updateTagById(id);
        return "redirect:/leader/showDocument";
    }


    //修改密码
    @RequestMapping(value = "/passwordRest")
    public String passwordRest() throws Exception {
        return "leader/passwordRest";
    }

}
